<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="./css/aui.css" />    
    <link rel="stylesheet" type="text/css" href="./css/aui-slide.css" />
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/doT.min.js"></script>
    <script type="text/javascript" src="./script/base.js"></script>
    <script type="text/javascript" src="./script/api.js"></script></head>
    <script type="text/javascript" src="./script/jquery-3.2.0.js"></script>
</head>
<body>

    <header class="header header-f" style="padding-top:2rem; height:4rem;">
        <div class="header-l aui-pull-left" onclick="closeWin()">
            <i class="fa fa-angle-left"></i>
        </div>
        <div class="header-title">
            我的下线
        </div>
        <div class="header-r aui-pull-right">
        </div>
    </header>
    <div style="height:4rem;"></div>

    <div id="list3">

    </div>

    <div id="list">

    </div>

</body>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/template" id="template3">
    <ul class="distribution-withdraw-nav">
        {{~it:value:index}}
        <li id="{{=value.type}}" onclick="changeSort({{=value.type}});">{{=value.name}}</li>
        {{~}}
    </ul>
</script>
<script type="text/template" id="template">
    {{~it:value:index}}
    <div class="team-t">
        <i class="fa fa-star-o"></i>代表已成为分销商的下线
    </div>
    <div class="team-item">
        <div class="team-item-l">
            <img src="{{=value.user_avatar}}">
            <dl>
                <dt>
                    <i class="fa fa-star-o"></i>
                    {{=value.parent_nickname}}
                </dt>
                <dd>成为分销商时间：{{=value.join_time}}</dd>
            </dl>
        </div>
        <div class="team-item-r">
            +{{=value.money}}
            <p>{{=value.agent}}个成员</p>
        </div>
    </div>
    {{~}}
</script>
<script type="text/javascript">

    apiready = function(){
        globalType = 1;
        loadData(globalType);
    }

    function loadData(type){
        var userid = $api.getStorage('userid');
        var authtoken = $api.getStorage('authtoken');
        api.ajax({
            url: webAddress + '/a/sale/UserHas',
            method: 'post',
            data: {
                values: {
                    type: type,
                },
            },
            headers: {
                TOKEN: token,
                authuid: userid,
                authtoken: authtoken,
                'Content-Type': 'application/x-www-form-urlencoded',
            },
        },function(ret, err){
            if (ret) {
                // alert( JSON.stringify( ret ) );

                // 时间戳转换为时间
                for (var i = 0; i < ret.result.data.length; i++) {
                 ret.result.data[i].join_time = toDate(ret.result.data[i].join_time);   
             }

             templateFun3(ret.result.saleList);
             templateFun(ret.result.data);
             $('#1').addClass('active');                
         } else {

            // alert( JSON.stringify( err ) );
            templateFun2();
            return false;
        }
    });
    }

        // 模板赋值函数(有数据)
        function templateFun(data){

            var template = $api.byId('template');
            var dotFn = doT.template(template.innerHTML);
            var html = dotFn(data);
            var list = $api.byId('list');
            $api.html(list, html);
        }

        // 模板赋值函数(无数据)
        function templateFun2(){
            var list = $api.byId('list');
            var html = '<h2 style="padding:1rem; text-align:center; color:#999; font-size:1rem;">暂无下线</h2>';
            list.innerHTML = html;
        }

        // 模板赋值函数(分类)
        function templateFun3(data){
            var template = $api.byId('template3');
            var dotFn = doT.template(template.innerHTML);
            var html = dotFn(data);
            var list = $api.byId('list3');
            $api.html(list, html);
        }

    // 改变分类
    function changeSort(sort){
        $('li').removeClass('active');
        $('#'+sort).addClass('active');
        globalType = sort;
        loadData(globalType);
    }

        // 字符串转时间
        function toDate(timestamp){
            // 宁哥提供代码
            // var newDate = new Date();
            // newDate.setTime(timestamp * 1000);
            // return newDate.toLocaleDateString();
            
            //时间戳为10位需*1000，时间戳为13位的话不需乘1000
            var date = new Date(timestamp * 1000);
            var Y = date.getFullYear() + '-';
            var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
            var D = date.getDate() + ' ';
            var h = date.getHours() + ':';
            var m = date.getMinutes() + ':';
            var s = date.getSeconds();
            return Y+M+D+h+m+s;
            // return Y+M+D;
        }

        function withdrawdetails(){
            var delay = 0;
            if(api.systemType != 'ios'){
                delay = 300;
            }
            api.openWin({
                name: 'withdrawdetails',
                url: './distribution-withdraw-details.html',
                bounces:false,
                delay: delay,
                slidBackEnabled:true,
                vScrollBarEnabled:false,
                progress:{
                    type:"page"
                }
            });
        }

        function closeWin(){
            api.closeWin({
            });
        }
    </script>
    </html>